package com.joysuch.wwyt.openapi.services.serviceV1.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.joysuch.wwyt.api.basedata.service.BpDepartSyncService;
import com.joysuch.wwyt.bp.entity.BpDepart;
import com.joysuch.wwyt.bp.enums.DataStateTypes;
import com.joysuch.wwyt.bp.enums.DepartType;
import com.joysuch.wwyt.bp.repository.BpDepartDao;
import com.joysuch.wwyt.core.enums.Status;
import com.joysuch.wwyt.core.service.BaseConfigService;
import com.joysuch.wwyt.core.service.BaseRoleDataAuthService;
import com.joysuch.wwyt.dzhapi.bean.ApiBaseUserBean;
import com.joysuch.wwyt.dzhapi.bean.ApiBpDepartBean;
import com.joysuch.wwyt.dzhapi.dto.BuildDataDto;
import com.joysuch.wwyt.dzhapi.mapper.ApiBaseUserMapper;
import com.joysuch.wwyt.dzhapi.mapper.ApiBpDepartMapper;
import com.joysuch.wwyt.openapi.controllers.controllerV1.req.ApiV1BpDepartAddReq;
import com.joysuch.wwyt.openapi.controllers.controllerV1.req.ApiV1BpDepartQueryListReq;
import com.joysuch.wwyt.openapi.controllers.controllerV1.req.ApiV1BpDepartTreeListReq;
import com.joysuch.wwyt.openapi.controllers.controllerV1.req.ApiV1BpDepartUpdateReq;
import com.joysuch.wwyt.openapi.controllers.controllerV1.vo.*;
import com.joysuch.wwyt.openapi.exception.ApiV1ExceptionCodeMsg;
import com.joysuch.wwyt.openapi.services.serviceV1.ApiV1BpDepartService;
import com.joysuch.wwyt.util.SnowFlakeUtil;
import com.joysuch.wwyt.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: JamesZY
 * @date: 2021/8/24
 * @description:
 */
@Service
public class ApiV1BpDepartServiceImpl implements ApiV1BpDepartService {
    @Autowired
    private BpDepartDao dao;
    @Autowired
    private BaseRoleDataAuthService dataAuthService;
    @Autowired
    private BpDepartSyncService departSyncService;
    @Autowired
    private BaseConfigService baseConfigService;
    @Autowired
    private ApiBpDepartMapper departMapper;
    @Autowired
    private ApiBaseUserMapper userMapper;

    @Override
    public List<ApiResultData> add(List<ApiV1BpDepartAddReq> data, BuildDataDto buildDataDto) {
        List<ApiResultData> list = Lists.newArrayList();
        data.forEach(d -> {
            ApiResultData resultData = new ApiResultData();
            if (StringUtil.isEmpty(d.getName())) {
                resultData.setUniqueId(d.getUniqueId());
                resultData.setName(d.getName());
                resultData.setMsg("部门名称不能为空");
                list.add(resultData);
                return;
            }
            if (StringUtil.isEmpty(d.getParentUniqueId())) {
                resultData.setUniqueId(d.getUniqueId());
                resultData.setName(d.getName());
                resultData.setMsg("上级部门唯一编码不能为空");
                list.add(resultData);
                return;
            }
            if (d.getName().length() > 64) {
                resultData.setUniqueId(d.getUniqueId());
                resultData.setName(d.getName());
                resultData.setMsg("部门名称长度不能超64字符");
                list.add(resultData);
                return;
            }
            if (d.getParentUniqueId().length() > 64) {
                resultData.setUniqueId(d.getUniqueId());
                resultData.setName(d.getName());
                resultData.setMsg("上级部门编码不能超64字符");
                list.add(resultData);
                return;
            }
            BpDepart depart = new BpDepart();
            BeanUtils.copyProperties(d, depart);
            if (ObjectUtil.isEmpty(d.getType())) {
                //0:group(集团),1:分公司，2：工厂,3:部门 默认为部门级
                depart.setType(DepartType.DEPART.getId());
            }
            if (StringUtil.isEmpty(d.getUniqueId())) {
                depart.setUniqueId(SnowFlakeUtil.getDepartId());
            } else {
                ApiBpDepartBean departBean = departMapper.findByUniqueId(d.getUniqueId(), buildDataDto.getTenentId(), buildDataDto.getOrgCode());
                if (ObjectUtil.isNotEmpty(departBean)) {
                    resultData.setUniqueId(d.getUniqueId());
                    resultData.setName(d.getName());
                    resultData.setMsg("部门唯一编码已存在");
                    list.add(resultData);
                    return;
                }
            }
            ApiBpDepartBean fatherDepartBean = new ApiBpDepartBean();
            if ("-1".equals(d.getParentUniqueId())) {
                fatherDepartBean = departMapper.getFatherDepart(buildDataDto.getOrgCode(), buildDataDto.getTenentId());
            } else {
                fatherDepartBean = departMapper.findByUniqueId(d.getParentUniqueId(), buildDataDto.getTenentId(), buildDataDto.getOrgCode());
            }
            if (ObjectUtil.isEmpty(fatherDepartBean)) {
                resultData.setUniqueId(d.getUniqueId());
                resultData.setName(d.getName());
                resultData.setMsg("父级部门不存在");
                list.add(resultData);
                return;
            }
            depart.setParentId(fatherDepartBean.getId());
            List<ApiBpDepartBean> parentList = departMapper.findByParentId(fatherDepartBean.getId(), buildDataDto.getTenentId(), buildDataDto.getOrgCode());
            for (ApiBpDepartBean bpDepart : parentList) {
                if (d.getName().equals(bpDepart.getName())) {
                    resultData.setUniqueId(d.getUniqueId());
                    resultData.setName(d.getName());
                    resultData.setMsg("此部门名称已存在");
                    list.add(resultData);
                    return;
                }
            }
            Integer no = fatherDepartBean.getChildNo();
            if (no == null) {
                no = 1;
            }
            depart.setChildNo(1);
            if (StringUtil.isEmpty(d.getCode())) {
                depart.setCode(fatherDepartBean.getCode() + "-" + StringUtils.leftPad(no+ "", 3, '0'));
            }
            depart.setOrgCode(buildDataDto.getOrgCode());
            depart.setTenentId(buildDataDto.getTenentId());
            BpDepart fatherDepart = new BpDepart();
            BeanUtils.copyProperties(fatherDepartBean, fatherDepart);
            // 调用同步接口，同步部门信息到人员在岗在位
            boolean sync = departSyncService.syncApiDepart(depart, fatherDepart, buildDataDto);
            if (sync) {
                depart.setLocateSync(true);
            } else {
                depart.setLocateSync(false);
            }
            ApiBpDepartBean save = new ApiBpDepartBean();
            BeanUtils.copyProperties(depart, save);
            departMapper.insert(save);
//            try {
//                checkDepartCode(save, buildDataDto);
//            } catch (Exception e) {
//                resultData.setUniqueId(d.getUniqueId());
//                resultData.setName(d.getName());
//                resultData.setMsg(e.getMessage());
//                list.add(resultData);
//                return;
//            }
            fatherDepartBean.setChildNo(no + 1);
            departMapper.updateById(fatherDepartBean);
            dataAuthService.initTenantRoleDepartCache();
            resultData.setUniqueId(save.getUniqueId());
            resultData.setName(save.getName());
            resultData.setMsg("添加成功");
            list.add(resultData);
        });
        return list;
    }

    @Override
    public List<ApiResultData> deleteByIds(List<String> data, BuildDataDto buildDataDto) {
        List<ApiResultData> resultData = Lists.newArrayList();
        data.forEach(id -> {
            ApiResultData d = new ApiResultData();
            ApiBpDepartBean dep = departMapper.findByUniqueId(id, buildDataDto.getTenentId(), buildDataDto.getOrgCode());
            if (ObjectUtil.isEmpty(dep)) {
                d.setUniqueId(id);
                d.setMsg("部门不存在");
                resultData.add(d);
                return;
            }
            //判断部门下是否有该人员/子部门
            List<ApiBaseUserBean> uList = userMapper.findByDepartId(dep.getId(), buildDataDto.getTenentId(), buildDataDto.getOrgCode());
            List<Long> dList = departMapper.findIdByByParentId(dep.getId(), buildDataDto.getTenentId(), buildDataDto.getOrgCode());
            if (!CollectionUtils.isEmpty(uList)) {
                d.setUniqueId(id);
                d.setName(dep.getName());
                d.setMsg("该部门存在人员，请先删除人员后再删除部门");
                resultData.add(d);
                return;
            }
            if (dList.size() > 0) {
                d.setUniqueId(id);
                d.setName(dep.getName());
                d.setMsg("该部门存在子部门，请先移除子部门后再删除部门");
                resultData.add(d);
                return;
            }
            // 调用同步接口，同步部门信息到人员在岗在位
            if (dep.getUniqueId() != null && baseConfigService.checkApiIfSync(buildDataDto)) {
                boolean b = departSyncService.deleteApiSync(dep.getUniqueId(), buildDataDto);
                dep.setLocateSync(b);
            }
            dep.setShowState(DataStateTypes.DELETE.getState());
            departMapper.updateById(dep);
            d.setUniqueId(dep.getUniqueId());
            d.setName(dep.getName());
            d.setMsg("删除成功");
            resultData.add(d);
        });

        return resultData;
    }

    @Override
    public void update(ApiV1BpDepartUpdateReq req, BuildDataDto buildDataDto) throws ApiV1ExceptionCodeMsg {
        ApiBpDepartBean departBean = departMapper.findByUniqueId(req.getUniqueId(), buildDataDto.getTenentId(), buildDataDto.getOrgCode());
        if (departBean == null) {
            throw new ApiV1ExceptionCodeMsg(Status.DEPART_NOT_EXIST);
        }
        if (StringUtil.notEmpty(req.getName())) {
            departBean.setName(req.getName());
            ApiBpDepartBean parentDepartBean = departMapper.selectById(departBean.getParentId());
            if (parentDepartBean != null) {
                List<ApiBpDepartBean> parentList = departMapper.findByParentId(parentDepartBean.getId(), buildDataDto.getTenentId(), buildDataDto.getOrgCode());
                for (ApiBpDepartBean d : parentList) {
                    if (d.getName().equals(req.getName()) && !d.getUniqueId().equals(departBean.getUniqueId())) {
                        throw new ApiV1ExceptionCodeMsg(Status.DEPART_NAME_EXIST);
                    }

                }
            }
        }
        if (StringUtil.notEmpty(req.getCode())) {
            departBean.setCode(req.getCode());
        }
        if (ObjectUtil.isNotEmpty(req.getType())) {
            departBean.setType(req.getType());
        }

        BpDepart oldParent = new BpDepart();
        BpDepart newParent = new BpDepart();
        ApiBpDepartBean oldParentBean = null;
        ApiBpDepartBean newParentBean = null;
        if (StringUtil.notEmpty(req.getParentUniqueId())) {
            oldParentBean = departMapper.findByIdAndStatus(departBean.getParentId(), buildDataDto.getTenentId(), buildDataDto.getOrgCode());

            newParentBean = departMapper.findByUniqueId(req.getParentUniqueId(), buildDataDto.getTenentId(), buildDataDto.getOrgCode());
            if (newParentBean == null || oldParentBean == null) {
                throw new ApiV1ExceptionCodeMsg(Status.PARENT_DEPART_NOT_EXIST);
            }

            departBean.setParentId(newParentBean.getId());

            BpDepart depart = new BpDepart();
            BeanUtils.copyProperties(departBean, depart);
            BeanUtils.copyProperties(newParentBean, newParent);

            boolean sync = departSyncService.syncApiDepart(depart, newParent, buildDataDto);
            if (sync) {
                departBean.setLocateSync(true);
            } else {
                departBean.setLocateSync(false);
            }
            departBean.setCode(newParentBean.getCode() + "-" + StringUtils.leftPad(newParentBean.getChildNo()+ "", 3, '0'));
            departMapper.updateById(departBean);
            //旧父部门的子部门数量减一
            oldParentBean.setChildNo(oldParentBean.getChildNo() - 1);
            departMapper.updateById(oldParentBean);
            newParentBean.setChildNo(newParentBean.getChildNo() + 1);
            departMapper.updateById(newParentBean);
        } else {
            oldParentBean = departMapper.findByIdAndStatus(departBean.getParentId(), buildDataDto.getTenentId(), buildDataDto.getOrgCode());
            BeanUtils.copyProperties(oldParentBean, oldParent);
            BpDepart depart = new BpDepart();
            BeanUtils.copyProperties(departBean, depart);
            boolean sync = departSyncService.syncApiDepart(depart, oldParent, buildDataDto);
            if (sync) {
                depart.setLocateSync(true);
            } else {
                depart.setLocateSync(false);
            }
            departMapper.updateById(departBean);
        }
    }

    @Override
    public ApiPagePropertyDataVo list(ApiV1BpDepartQueryListReq req, BuildDataDto buildDataDto) {
        QueryWrapper<ApiBpDepartBean> queryWrapper = new QueryWrapper<>();
//        QBpDepart bpDepart1 = QBpDepart.bpDepart1;
//        Predicate predicate = bpDepart1.isNotNull();
//        predicate = ExpressionUtils.and(predicate,
//                ExpressionUtils.or(bpDepart1.showState.notIn(DataStateTypes.DELETE.getState()),
//                        bpDepart1.showState.isNull()));
        if (ObjectUtil.isNotEmpty(req.getStartUpdateTime())) {
            queryWrapper.ge("UPDATE_TIME", req.getStartUpdateTime());
        }
        if (ObjectUtil.isNotEmpty(req.getEndUpdateTime())) {
            queryWrapper.le("UPDATE_TIME", req.getEndUpdateTime());
        }
        if (StringUtil.notEmpty(req.getName())) {
            queryWrapper.like("NAME", "%" + req.getName() + "%");
        }
        if (ObjectUtil.isNotEmpty(req.getType())) {
            queryWrapper.eq("type", req.getType());
        }
        if (StringUtil.notEmpty(req.getUniqueId())) {
            queryWrapper.eq("unique_id", req.getUniqueId());
        }
        queryWrapper.eq("TENENT_ID", buildDataDto.getTenentId());
        queryWrapper.eq("ORG_CODE", buildDataDto.getOrgCode());
        queryWrapper.eq("DELETE_FLAG", "0");
        queryWrapper.and(QueryWrapper -> QueryWrapper.isNull("show_state").or().eq("show_state", "1"));

        com.baomidou.mybatisplus.extension.plugins.pagination.Page<ApiBpDepartBean> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(req.getPageNumber(), req.getPageSize());
        departMapper.selectPage(page, queryWrapper);
        List<ApiV1BpDepartVo> data = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(page.getRecords())) {
            for (ApiBpDepartBean depart : page.getRecords()) {
                ApiV1BpDepartVo vo = new ApiV1BpDepartVo();
                vo.setUniqueId(depart.getUniqueId());
                vo.setName(depart.getName());
                vo.setType(depart.getType());
                if (ObjectUtil.isNotEmpty(depart.getParentId())) {
                    ApiBpDepartBean departBean = departMapper.selectById(depart.getParentId());
                    if (departBean != null) {
                        vo.setParentName(departBean.getName());
                        vo.setParentUniqueId(departBean.getUniqueId());
                    }
                }
                vo.setUpdateTime(depart.getUpdateTime());
                vo.setCreateTime(depart.getCreateTime());
                data.add(vo);
            }
        }
        ApiPagePropertyDataVo apiPagePropertyDataVo = new ApiPagePropertyDataVo();
        apiPagePropertyDataVo.setData(data);
        apiPagePropertyDataVo.setTotalElements(page.getTotal());
        apiPagePropertyDataVo.setTotalPages((int) page.getPages());
        apiPagePropertyDataVo.setPresentPage((int) page.getCurrent());
        return apiPagePropertyDataVo;
    }

    @Override
    public List<ApiV1BpDepartTreeListVo> treeList(ApiV1BpDepartTreeListReq req, BuildDataDto buildDataDto) {
        List<ApiV1BpDepartTreeListVo> treeList = Lists.newArrayList();
        if (req.getHasSubs()) {
            treeList = getDepartList(req, buildDataDto);
        } else {
            treeList = getSonList(req, buildDataDto);
        }
        return treeList;
    }

    private List<ApiV1BpDepartTreeListVo> getSonList(ApiV1BpDepartTreeListReq req, BuildDataDto dto) {
        QueryWrapper<ApiBpDepartBean> queryWrapper = new QueryWrapper<>();
        List<ApiV1BpDepartTreeListVo> data = Lists.newArrayList();
        if (StringUtil.notEmpty(req.getUniqueId()) || StringUtil.notEmpty(req.getName())) {
            if (StringUtil.notEmpty(req.getName())) {
                queryWrapper.like("NAME", "%" + req.getName() + "%");
            }
            if (StringUtil.notEmpty(req.getUniqueId())) {
                queryWrapper.eq("unique_id", req.getUniqueId());
            }
            queryWrapper.eq("TENENT_ID", dto.getTenentId());
            queryWrapper.eq("ORG_CODE", dto.getOrgCode());
            queryWrapper.eq("DELETE_FLAG", "0");
            queryWrapper.and(QueryWrapper -> QueryWrapper.isNull("show_state").or().eq("show_state", "1"));
            List<ApiBpDepartBean> all = departMapper.selectList(queryWrapper);

            if (!CollectionUtils.isEmpty(all)) {
                all.forEach(depart -> {
                    ApiV1BpDepartTreeListVo parentVo = new ApiV1BpDepartTreeListVo();
                    BeanUtils.copyProperties(depart, parentVo);
                    parentVo.setCreateTime(depart.getCreateTime());
                    parentVo.setUpdateTime(depart.getUpdateTime());
                    if (ObjectUtil.isNotEmpty(depart.getParentId())) {
                        ApiBpDepartBean departBean = departMapper.selectById(depart.getParentId());
                        if (departBean != null) {
                            parentVo.setParentName(departBean.getName());
                            parentVo.setParentUniqueId(departBean.getUniqueId());
                        }
                    }
                    List<ApiV1BpDepartTreeListVo> childs = Lists.newArrayList();
                    List<ApiBpDepartBean> sonDepartList = departMapper.findByParentId(depart.getId(), dto.getTenentId(), dto.getOrgCode());
                    if (!CollectionUtils.isEmpty(sonDepartList)) {
                        sonDepartList.forEach(d -> {
                            ApiV1BpDepartTreeListVo vo = new ApiV1BpDepartTreeListVo();
                            BeanUtils.copyProperties(d, vo);
                            vo.setCreateTime(d.getCreateTime());
                            vo.setUpdateTime(d.getUpdateTime());
                            ApiBpDepartBean departBean = departMapper.selectById(d.getParentId());
                            if (departBean != null) {
                                vo.setParentName(departBean.getName());
                                vo.setParentUniqueId(departBean.getUniqueId());
                            }
                            childs.add(vo);
                        });
                    }
                    parentVo.setChilds(childs);
                    data.add(parentVo);
                });
            }
            return data;
        }
        ApiBpDepartBean fatherDepart = departMapper.getFatherDepart(dto.getOrgCode(), dto.getTenentId());
        if (fatherDepart != null) {
            ApiV1BpDepartTreeListVo parentVo = new ApiV1BpDepartTreeListVo();
            BeanUtils.copyProperties(fatherDepart, parentVo);
            List<ApiV1BpDepartTreeListVo> childs = Lists.newArrayList();
            List<ApiBpDepartBean> sonDepartList = departMapper.findByParentId(fatherDepart.getId(), dto.getTenentId(), dto.getOrgCode());
            if (!CollectionUtils.isEmpty(sonDepartList)) {
                sonDepartList.forEach(d -> {
                    ApiV1BpDepartTreeListVo vo = new ApiV1BpDepartTreeListVo();
                    BeanUtils.copyProperties(d, vo);
                    Optional<BpDepart> optional = dao.findById(d.getParentId());
                    if (optional.isPresent()) {
                        vo.setParentName(optional.get().getName());
                        vo.setParentUniqueId(optional.get().getUniqueId());
                    }
                    childs.add(vo);
                });
            }
            parentVo.setChilds(childs);
            data.add(parentVo);
        }
        return data;
    }

    /**
     * 获取部门树形结构
     *
     * @return
     */
    private List<ApiV1BpDepartTreeListVo> getDepartList(ApiV1BpDepartTreeListReq req, BuildDataDto dto) {
        List<ApiV1BpDepartTreeListVo> returnList = new ArrayList<>();
        List<ApiBpDepartBean> all = Lists.newArrayList();
        QueryWrapper<ApiBpDepartBean> queryWrapper = new QueryWrapper<>();
        if (StringUtil.notEmpty(req.getUniqueId()) || StringUtil.notEmpty(req.getName())) {
            if (StringUtil.notEmpty(req.getName())) {
                queryWrapper.like("NAME", req.getName());
            }
            if (StringUtil.notEmpty(req.getUniqueId())) {
                queryWrapper.eq("unique_id", req.getUniqueId());
            }
            queryWrapper.eq("TENENT_ID", dto.getTenentId());
            queryWrapper.eq("ORG_CODE", dto.getOrgCode());
            queryWrapper.eq("DELETE_FLAG", "0");
            queryWrapper.and(QueryWrapper -> QueryWrapper.isNull("show_state").or().eq("show_state", "1"));
            all = departMapper.selectList(queryWrapper);
            if (all != null) {
                List<ApiV1BpDepartTreeListVo> list = all.stream().map(r -> {
                    ApiV1BpDepartTreeListVo bean = new ApiV1BpDepartTreeListVo();
                    BeanUtils.copyProperties(r, bean);
                    bean.setUpdateTime(r.getUpdateTime());
                    bean.setCreateTime(r.getCreateTime());
                    if (ObjectUtil.isNotEmpty(r.getParentId())) {
                        ApiBpDepartBean departBean = departMapper.selectById(r.getParentId());
                        if (departBean != null) {
                            bean.setParentName(departBean.getName());
                            bean.setParentUniqueId(departBean.getUniqueId());
                        }
                    }
                    return bean;
                }).collect(Collectors.toList());
                list.forEach(d -> {
                    List<ApiV1BpDepartTreeListVo> findget = findget(d.getId(), dto);
                    d.setChilds(findget);
                });
                returnList.addAll(list);
            }
        } else {
            List<ApiBpDepartBean> departs = departMapper.findByParentIsNull(dto.getTenentId(), dto.getOrgCode());
            List<ApiV1BpDepartTreeListVo> list = departs.stream().map(r -> {
                ApiV1BpDepartTreeListVo bean = new ApiV1BpDepartTreeListVo();
                BeanUtils.copyProperties(r, bean);
                bean.setUpdateTime(r.getUpdateTime());
                bean.setCreateTime(r.getCreateTime());
                if (ObjectUtil.isNotEmpty(r.getParentId())) {
                    ApiBpDepartBean departBean = departMapper.selectById(r.getParentId());
                    if (departBean != null) {
                        bean.setParentName(departBean.getName());
                        bean.setParentUniqueId(departBean.getUniqueId());
                    }
                }
                return bean;
            }).collect(Collectors.toList());
            list.forEach(d -> {
                List<ApiV1BpDepartTreeListVo> findget = findget(d.getId(), dto);
                d.setChilds(findget);
            });
            returnList.addAll(list);
        }
        return returnList;
    }

    private List<ApiV1BpDepartTreeListVo> findget(Long id, BuildDataDto dto) {
        QueryWrapper<ApiBpDepartBean> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PARENT_ID", id);
        queryWrapper.eq("TENENT_ID", dto.getTenentId());
        queryWrapper.eq("ORG_CODE", dto.getOrgCode());
        queryWrapper.eq("DELETE_FLAG", "0");
        queryWrapper.and(QueryWrapper -> QueryWrapper.isNull("show_state").or().eq("show_state", "1"));
        ArrayList<ApiV1BpDepartTreeListVo> list = new ArrayList<>();
        List<ApiBpDepartBean> all = departMapper.selectList(queryWrapper);
        if (all != null) {
            for (ApiBpDepartBean bpDepart : all) {
                ApiV1BpDepartTreeListVo vo = new ApiV1BpDepartTreeListVo();
                vo.setId(bpDepart.getId());
                vo.setUniqueId(bpDepart.getUniqueId());
                vo.setType(bpDepart.getType());
                vo.setName(bpDepart.getName());
                vo.setCreateTime(bpDepart.getCreateTime());
                vo.setUpdateTime(bpDepart.getUpdateTime());
                if (ObjectUtil.isNotEmpty(bpDepart.getParentId())) {
                    ApiBpDepartBean departBean = departMapper.selectById(bpDepart.getParentId());
                    if (departBean != null) {
                        vo.setParentName(departBean.getName());
                        vo.setParentUniqueId(departBean.getUniqueId());
                    }
                }
                list.add(vo);
                // 进行循环查询子部门，直到没有时跳出循环
                List<ApiV1BpDepartTreeListVo> findget = findget(bpDepart.getId(), dto);
                vo.setChilds(findget);
//                list.addAll(findget);
            }
            return list;
        }
        return null;
    }

    private void checkDepartCode(ApiBpDepartBean depart, BuildDataDto dto) {
        int count = departMapper.countByCodeAndState(depart.getCode(), dto.getTenentId(), dto.getOrgCode());
        if (count > 1) {
            throw new IllegalArgumentException("该部门编码已存在");
        }
    }

}
